﻿using System.Security.Claims;
using Csb.SignalServer.Model;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Csb.SignalServer.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace Csb.SignalServer.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class AccountController : ControllerBase
    {
        private readonly IHubContext<ChatHub> _hubContext;

        public AccountController(IHubContext<ChatHub> hubContext)
        {
            _hubContext = hubContext;
        }


        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login([FromBody]User user)
        {
            // 这里进行用户验证，假设验证成功
            List<User> users = new List<User>();
            users.Add(new User { UserName = "zhangsan", UserPassord = "123456" });
            users.Add(new User { UserName = "lisi", UserPassord = "123456" });
            users.Add(new User { UserName = "wangwu", UserPassord = "123456" });

            if (users.Where(t=>t.UserName == user.UserName && t.UserPassord == user.UserPassord).Count() > 0)
            {
                await Task.Delay(100); // 模拟延迟
                return Ok(new { Message = "登录成功！" });
            }
            ModelState.AddModelError(string.Empty, "用户名或密码不正确");
            return BadRequest(ModelState);
        }

        [HttpGet]
        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return RedirectToAction("Login", "Account");
        }


    }
}
